﻿using Microsoft.AspNetCore.Http;
using SkiaSharp;
using System;
using System.Drawing;
using System.IO;

namespace Colorful.Image
{
    public class ColorImage
    {
        private string _path;
        public ColorImage() { }
        /// <summary>
        /// 图片操作类
        /// </summary>
        /// <param name="path">要打开/保存的图片路径</param>
        public ColorImage(string path)
        {
            _path = path;
        }

        /// <summary>
        /// 获取图片保存路径
        /// </summary>
        public string SavePath
        {
            get
            {
                return _path;
            }
        }

        /// <summary>
        /// 压缩图片
        /// </summary>
        /// <param name="maxSize">图片最大宽高</param>
        /// <param name="quality">压缩率</param>
        /// <param name="imageFormat">要保存的图片格式</param>
        public ColorImage Compress(string filePath, Size? maxSize = null, int quality = 80, ImageFormat? imageFormat = null)
        {
            if (string.IsNullOrEmpty(_path))
                _path = filePath;
            using (var stream = new MemoryStream(File.ReadAllBytes(filePath)))
            {
                return Compress(stream, maxSize, quality, imageFormat);
            }
        }
        /// <summary>
        /// 压缩图片
        /// </summary>
        /// <param name="formFile">上传的文件对象</param>
        /// <param name="maxSize">图片最大宽高</param>
        /// <param name="quality">压缩率</param>
        /// <param name="imageFormat">要保存的图片格式</param>
        public ColorImage Compress(IFormFile formFile, Size? maxSize = null, int quality = 80, ImageFormat? imageFormat = null)
        {
            using (var stream = new MemoryStream())
            {
                formFile.CopyTo(stream);
                return Compress(stream, maxSize, quality, imageFormat);
            }
        }

        /// <summary>
        /// 压缩图片
        /// </summary>
        /// <param name="stream">文件流</param>
        /// <param name="savePath">保存路径</param>
        /// <param name="maxSize">图片最大宽高</param>
        /// <param name="quality">压缩率</param>
        /// <param name="imageFormat">要保存的图片格式</param>
        public ColorImage Compress(Stream stream, Size? maxSize = null, int quality = 80, ImageFormat? imageFormat = null)
        {
            if (_path.EndsWith(".gif", StringComparison.OrdinalIgnoreCase))
            {
                using (var inputStream = new SKManagedStream(stream))
                {
                    var buffer = new byte[inputStream.Length];
                    inputStream.Read(buffer, buffer.Length);
                    System.IO.File.WriteAllBytes(_path, buffer);
                }
                return this;
            }
            using (var inputStream = new SKManagedStream(stream))
            {
                using (var original = SKBitmap.Decode(inputStream))
                {
                    if (imageFormat == null)
                    {
                        if (original.Info.IsOpaque)
                        {
                            imageFormat = ImageFormat.Jpeg;
                            _path = _path.Replace(".png", ".jpg");
                        }
                        else
                        {
                            imageFormat = ImageFormat.Png;
                            _path = _path.Replace(".jpg", ".png");
                        }
                    }
                    SKBitmap bitmap;
                    if (maxSize != null && !maxSize.Value.IsEmpty && (original.Width > maxSize.Value.Width || original.Height > maxSize.Value.Height))
                    {
                        int width, height;
                        if (original.Width > original.Height)
                        {
                            var size = maxSize.Value.Width;
                            width = size;
                            height = original.Height * size / original.Width;
                        }
                        else
                        {
                            var size = maxSize.Value.Height;
                            width = original.Width * size / original.Height;
                            height = size;
                        }
                        bitmap = original.Resize(new SKImageInfo(width, height), SKBitmapResizeMethod.Lanczos3);
                    }
                    else
                    {
                        bitmap = original;
                    }
                    using (var image = SKImage.FromBitmap(bitmap))
                    {
                        using (var output = File.OpenWrite(_path))
                        {
                            image.Encode((SKEncodedImageFormat)imageFormat.Value, quality)
                                .SaveTo(output);
                        }
                    }
                }
            }
            return this;
        }
    }
}
